package com.dalei.vdsmanager.security.dao;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class RoleDaoImpl extends HibernateDaoSupport implements RoleDao,
		Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7457284540098799934L;

	@Override
	public void saveRole(Role role) {
		// TODO Auto-generated method stub
		getHibernateTemplate().save(role);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Role> findRoles() {
		// TODO Auto-generated method stub
		String hql = "FROM Role ORDER BY createdTime";
		List<Role> roles = getHibernateTemplate().find(hql);
		return roles;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Role> findRolesByCreator(User creator) {
		// TODO Auto-generated method stub
		String hql = "FROM Role WHERE createdBy = ? ORDER BY createdTime";
		List<Role> roles = getHibernateTemplate().find(hql, creator.getId());
		return roles;
	}

	@Override
	public Role findRoleById(int id) {
		// TODO Auto-generated method stub
		Role role = (Role) getHibernateTemplate().get(Role.class, id);
		return role;
	}

	@Override
	public Role findRoleByRoleName(String roleName) {
		// TODO Auto-generated method stub
		Criteria criteria = getSession().createCriteria(Role.class);
		criteria.add(Restrictions.eq("roleName", roleName));
		Role role = (Role) criteria.uniqueResult();
		return role;
	}

	@Override
	public void updateRole(Role role) {
		// TODO Auto-generated method stub
		getHibernateTemplate().update(role);
	}

	@Override
	public void deleteRole(Role role) {
		// TODO Auto-generated method stub
		getHibernateTemplate().delete(role);
	}

}
